﻿using Prism.Ioc;
using System.Windows;

namespace NxChatQQ.Services
{
    public class WindowsProviderService
    {
        private readonly IContainerProvider _serviceProvider;

        public WindowsProviderService(IContainerProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public void Show<T>() where T : Window
        {
            Window windowInstance = GetInstanceWindows<T>();

            if (windowInstance != null)
            {
                windowInstance.Activate();
            }
            else
            {
                windowInstance = _serviceProvider.Resolve<T>();
                windowInstance.Owner = Application.Current.MainWindow;
                windowInstance.Show();
                windowInstance.Activate();
            }
        }

        public Window GetInstanceWindows<T>()
        {
            foreach (Window item in Application.Current.Windows)
            {
                if (item is T)
                {
                    return item;
                }
            }

            return null;
        }

    }
}
